package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.service.IBusNeighborhoodLedgerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusBldg;
import com.ruoyi.business.service.IBusBldgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼栋管理Controller
 * 
 * @author ling
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/business/bldg")
public class BusBldgController extends BaseController
{
    @Autowired
    private IBusBldgService busBldgService;

    @Value("${export.max:10000}")
    private Integer max;

    /**
     * 查询楼栋管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:bldg:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBldg busBldg)
    {
        startPage();
        List<BusBldg> list = busBldgService.selectBusBldgList(busBldg);
        return getDataTable(list);
    }

    /**
     * 导出楼栋管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:bldg:export')")
    @Log(title = "楼栋管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBldg busBldg)
    {
        List<BusBldg> list = busBldgService.selectBusBldgList(busBldg);
        if (list.size() > max) {
            throw new ServiceException("导出失败！,每次最多导出 " + max + "条");
        }
        ExcelUtil<BusBldg> util = new ExcelUtil<BusBldg>(BusBldg.class);
        util.exportExcel(response, list, "楼栋管理数据");
    }

    /**
     * 获取楼栋管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:bldg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busBldgService.selectBusBldgById(id));
    }

    /**
     * 新增楼栋管理
     */

    @PreAuthorize("@ss.hasPermi('business:bldg:add')")
    @Log(title = "楼栋管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusBldg busBldg)
    {
//        新增楼栋后修改小区表的楼栋数量字段。

//        判断新增的楼栋是哪个小区的，并在小区表中更新对应楼栋数量。

        return toAjax(busBldgService.insertBusBldg(busBldg));
    }

    /**
     * 修改楼栋管理
     */
    @PreAuthorize("@ss.hasPermi('business:bldg:edit')")
    @Log(title = "楼栋管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBldg busBldg)
    {
        return toAjax(busBldgService.updateBusBldg(busBldg));
    }

    /**
     * 删除楼栋管理
     */
    @PreAuthorize("@ss.hasPermi('business:bldg:remove')")
    @Log(title = "楼栋管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //        删除楼栋后修改小区表的楼栋数量字段。
        //        判断新增的楼栋是哪个小区的，并在小区表中更新对应楼栋数量。

        return toAjax(busBldgService.deleteBusBldgByIds(ids));
    }
}
